package com.woan.api.servlet;

import java.io.IOException;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.woan.api.model.SysAdmininfo;
import com.woan.api.model.SysVideo;
import com.woan.api.model.SysVideoBasic;
import com.woan.api.services.UserClientServices;
import com.woan.api.services.VideoClientServices;
import com.woan.api.util.FuncDate;
import com.woan.api.util.HttpTool;

import net.sf.json.JSONObject;

public class VideoLiveUFlv extends HttpServlet {
	private static final long serialVersionUID = 1L;
	HttpTool ht = new HttpTool();
	protected FuncDate fd = null;

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException {
		Hashtable<String, String> hash = null;
		hash = this.ht.getRequestBody(request);
		System.out.println("视频服务器发送的内容---------"+hash);
		String str1 = (String) hash.get("type");
		if(str1.equals("0"))
		{	
			videoFlvStart(request, response,hash);
		}		
//		if (str1.equals("1"))
//		{
//			videoStop(request, response);
//		}
				
		if (str1.equals("2"))
		{	
			videoArchive(request, response,hash);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public void init() throws ServletException {
	}
	public void videoFlvStart(HttpServletRequest request,
			HttpServletResponse response,Hashtable<String, String> hash) throws ServletException, IOException {
		System.out.println("----------视频FlV直播开始-----------");		
		System.out.println("----------视频FlV直播消息内容-----------" + hash);
		String vid = (String) hash.get("hashed_id");
		//获取指派任务的Id
		String task_id=(String)hash.get("service_code");
		String user_name = "liveu|liveu01";
		String videoPath = (String) hash.get("url");
		String videoSize = (String) hash.get("size");
		String groupname = "";
		String username = "";
		if (user_name.contains("|")) 
		{
			String para[] = user_name.split("\\|");
			groupname = para[0];
			username = para[1];
		}
		ServletContext servletContext = this.getServletContext();     
        WebApplicationContext wac = null;     
        wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);    
        UserClientServices userClientServices = (UserClientServices) wac.getBean("userClientServices");
        VideoClientServices videoClientServices = (VideoClientServices) wac.getBean("videoClientServices");
        SysAdmininfo user = null;
    	System.out.println("++++++++++++++++++++++++更新任务了开始1+++++++++++++++++++++++++++++++++++++++++++");
        if (!"XZB".equals(task_id)) {
        	System.out.println("++++++++++++++++++++++++更新任务了开始进去了———+安卓++++++++++++++++++++++++++++++++++++++++++++");
        	SysVideoBasic videoBasic_task = videoClientServices.getSysvideobasiciDAO().get(task_id);
        	SysVideoBasic videoBasic = videoClientServices.getBasicVideoByVid(vid);
        	String title = videoBasic_task.getTitle();
        	String detail = videoBasic_task.getDetail();
        	videoBasic.setDetail(detail);
        	videoBasic.setTitle(title);
        	System.out.println("++++++++++++++++++++++++更新任务结束了+++++++++++++++++++++++++++++++++++++++++++");
		}
        
        user = userClientServices.findUserByGroupNameAndUserName(groupname, username);
		String uid = "";
		if (user != null) {
			uid = user.getUid();
			//videoClientServices.updateVideoState(uid);
		} 
		SysVideo sysVideo1 = videoClientServices.listOneVideoinfo(vid, "android");
		//if(sysVideo1==null)
		//{
			SysVideo sysVideo = new SysVideo();
			sysVideo.setVid(vid);
			sysVideo.setBeginTime(FuncDate.FindCurSysTime1());
			sysVideo.setEndTime("");
			sysVideo.setPicture("");
			sysVideo.setStatus(0);
			sysVideo.setVideoLength("");
			sysVideo.setVideoPath(videoPath);
			sysVideo.setVideoSize(videoSize);
			sysVideo.setVideoType("android");
			sysVideo.setIsdel("1");
			//System.out.println("下面push就开始了！");
			//videoClientServices.pushMessageFlv(sysVideo,user.getSysRole().getRid(),groupname,username);
			//System.out.println("下面push就结束了！");
			System.out.println("flv直播插入视频");
			videoClientServices.editVideos(user.getSysRole().getRid(),user.getUid(),user.getUserName(),vid, "暂无标题","","","","","","","","","");
			videoClientServices.insertVideo(sysVideo,user.getSysRole().getRid(),groupname,username);
			
		//}
	}
	public void videoStop(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
//		System.out.println("---------------- FLV视频停止通知 -----------");
//		System.out.println("----------this.hash-----------" + this.hash);
//		String video_code = (String) this.hash.get("hashed_id");
//		String curtime = FuncDate.FindCurSysTime1();
//		VOESysVideo video = new VOESysVideo();
//		BOQVideo boqvideo = new BOQVideo();
//		video = boqvideo.FindvideoByHashid(video_code);
//		video.setVid(video_code);
//		video.setEndtime(curtime);
//		video.setStatusFlv("1");
//		BLVideo blvideo = new BLVideo();
//		
//		String resu = "0";
//		resu = blvideo.updateVideo(video);
//		String msg = "";
//		if (resu.equals("0")) {
//			msg = "0";
//			this.ht.retData(response, msg);
//		} else {
//			msg = "-1";
//			this.ht.retData(response, msg);
//		}
	}

	public void videoArchive(HttpServletRequest request,
			HttpServletResponse response,Hashtable<String, String> hash) throws ServletException, IOException {
		System.out.println("---------------- FLV存档通知 -----------");
		System.out.println("----------FLV存档通知消息内容-----------" + hash);
		String vid = (String) hash.get("hashed_id");
		String videoLength = (String) hash.get("duration");	
		String videoPath = (String) hash.get("url");
		String curtime = FuncDate.FindCurSysTime1();
		String user_name = "liveu|liveu01";
		String groupname = "";
		String username = "";
		if (user_name.contains("|")) 
		{
			String para[] = user_name.split("\\|");
			groupname = para[0];
			username = para[1];
		}
		ServletContext servletContext = this.getServletContext();     
        WebApplicationContext wac = null;     
        wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);    
        UserClientServices userClientServices = (UserClientServices) wac.getBean("userClientServices");
        VideoClientServices videoClientServices = (VideoClientServices) wac.getBean("videoClientServices");
        SysAdmininfo user = null;
        user = userClientServices.findUserByGroupNameAndUserName(groupname, username);
        List<SysVideo> sysVideo1 = videoClientServices.listVideoinfo(vid, "android");	
        if(sysVideo1.size()>0)
        {
        	for(int i=0;i<sysVideo1.size();i++)
        	{
	        	SysVideo sysVideo = sysVideo1.get(i);
				sysVideo.setVideoPath(videoPath);
				sysVideo.setPicture(videoPath+".jpg");
				sysVideo.setStatus(1);
				sysVideo.setEndTime(FuncDate.FindCurSysTime1());
				sysVideo.setVideoLength(videoLength);
				videoClientServices.updateVideo(sysVideo,user.getSysRole().getRid(),groupname,username);
        	}
        }
		System.out.println("flv存档更新视频");

	}
}